package com.fox.webapp.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StreamConvertUtil {

	public static ByteArrayOutputStream parse(InputStream in) throws IOException{
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		int ch;
		while ((ch = in.read()) != -1) {
			swapStream.write(ch);
		}
		return swapStream;
	}
	
	public static ByteArrayInputStream parse(OutputStream out){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		baos = (ByteArrayOutputStream)out;
		ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
		return swapStream;
	}
	
	public static String parseToString(InputStream in){
		return parseToString(in).toString();
	}
	
	public static String parseToString(OutputStream out){
		return parse(out).toString();
	}
	
	public ByteArrayInputStream parseInputStream(String in){
		ByteArrayInputStream input = new ByteArrayInputStream(in.getBytes());
		return input;
	}
	
	public ByteArrayOutputStream parseOutputStream(String in) throws IOException{
		return parse(parseInputStream(in));
	}
}
